import unittest

import requests

from api import log
from api.api_approve_trust import ApiApproveTrust
from api.api_register_login import ApiRegisterLogin
from bs4 import BeautifulSoup
from parameterized import parameterized
from utils import parser_html, read_json


class TestApproveTrust(unittest.TestCase):
    def setUp(self) -> None:
        # 1.获取session
        self.session = requests.session()
        # 2.获取接口对象
        self.approve = ApiApproveTrust(self.session)
        # 3.调用登录成功
        ApiRegisterLogin(self.session).api_login()

    def tearDown(self) -> None:
        self.session.close()

    # 认证接口测试
    def test01_approve(self):
        try:
            resp = self.approve.api_approve()
            log.info('执行接口结果为:{}'.format(resp.text))
            self.assertIn('提交成功', resp.text)
            log.info('断言成功')
        except Exception as e:
            log.error('断言失败:{}'.format(e))
            raise

    # 获取认证信息接口测试
    def test02_approve_status(self):
        try:
            resp = self.approve.api_approve_status()
            log.info('执行接口结果为:{}'.format(resp.text))
            self.assertIn('张', resp.text)
            log.info('断言成功')
        except Exception as e:
            log.error('断言失败:{}'.format(e))
            raise

    # 开户接口测试
    def test03_trust(self):
        try:
            resp = self.approve.api_trust()
            print('请求后台开户结果', resp.json())
            log.info('执行接口结果为:{}'.format(resp.json()))
            self.assertIn('form', resp.text)
            log.info('断言成功')
            # 三方开户
            res = parser_html(resp)
            print('三方开户提取的结果为:', res)
            resp = self.session.post(url=res[0], data=res[1])
            print('三方开户结果为:', resp.text)
            log.info('执行接口结果为:{}'.format(resp.text))
            self.assertIn('OK', resp.text)
            log.info('断言成功')
        except Exception as e:
            log.error('断言失败:{}'.format(e))
            raise

    # 获取充值验证码接口测试
    @parameterized.expand(read_json("approve_trust.json", "img_code"))
    def test04_img_code(self, random, expect_data):
        try:
            resp = self.approve.api_img_code(random)
            log.info('执行接口结果为:{}'.format(resp.status_code))
            self.assertEqual(expect_data, resp.status_code)
            log.info('断言成功')
        except Exception as e:
            log.error('断言失败:{}'.format(e))
            raise

    # 充值接口测试
    @parameterized.expand(read_json("approve_trust.json", "recharge"))
    def test05_recharge(self, valicode, expect_data):
        try:
            # 调用短信验证码
            self.approve.api_img_code(123)
            resp = self.approve.api_recharge(valicode)
            print('请求后台充值结果为:{}'.format(resp.json()))
            log.info('执行接口结果为:{}'.format(resp.json()))
            if valicode == 8888:

                self.assertIn('form', resp.text)
                log.info('断言成功')
                # 三方充值
                res = parser_html(resp)
                print('三方充值提取的结果为:', res)
                resp = self.session.post(url=res[0], data=res[1])
                print('三方开户结果为:', resp.text)
                log.info('执行接口结果为:{}'.format(resp.text))
                self.assertIn(expect_data, resp.text)
                log.info('断言成功')
            else:
                self.assertIn(expect_data, resp.text)
        except Exception as e:
            log.error('断言失败:{}'.format(e))
            raise
